package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.DiscussContentParam;
import com.macro.mall.dto.UsersManParam;
import com.macro.mall.mapper.SpsDataContentMapper;
import com.macro.mall.mapper.SpsDataDiscussMapper;
import com.macro.mall.mapper.admin.BasicUserPortraitMapper;
import com.macro.mall.mapper.admin.BasicUsersManMapper;
import com.macro.mall.model.admin.BasicUsersMan;
import com.macro.mall.pojo.BasicUsersManResult;
import com.macro.mall.pojo.BasicUsersManVo;
import com.macro.mall.pojo.DiscussContentVo;
import com.macro.mall.pojo.SpsDataContentVo;
import com.macro.mall.query.UsersManQuery;
import com.macro.mall.service.SysUsersManService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class SysUsersManServiceImpl implements SysUsersManService {

    @Autowired
    private BasicUsersManMapper usersManMapper;

    @Autowired
    private BasicUserPortraitMapper userPortraitMapper;

    @Autowired
    private SpsDataDiscussMapper dataDiscussMapper;

    @Autowired
    private SpsDataContentMapper dataContentMapper;

    /***
     * 获取用户管理列表
     * @param param
     * @return
     */
    @Override
    public CommonResult getList(UsersManParam param) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(param.getPage(), param.getPageSize());
        UsersManQuery query = new UsersManQuery();
        BeanUtils.copyProperties(param, query);
        if (param.getRegisterTime() != null && param.getRegisterTime().length > 0) {
            query.setCreateTime(param.getRegisterTime()[0]);
            query.setEndTime(param.getRegisterTime()[1]);
        }
        if (param.getPortraitLabel() != null && param.getPortraitLabel().length > 0) {
            String join = StringUtils.join(param.getPortraitLabel(), ",");
            query.setPortraits(join);
        }
        List<BasicUsersManResult> usersManVos = usersManMapper.selectList(query);

        List<BasicUsersManVo> basicUsersManVos = new ArrayList<>();
        for (BasicUsersMan usersManVo : usersManVos) {
            BasicUsersManVo basicUsersManVo = new BasicUsersManVo();
            BeanUtils.copyProperties(usersManVo, basicUsersManVo);
            if (usersManVo.getPortraitLabel() != null && usersManVo.getPortraitLabel().length() > 0 && !usersManVo.getPortraitLabel().equals("")) {
                String[] split = usersManVo.getPortraitLabel().split(",");
                List<String> list = Arrays.asList(split);
                basicUsersManVo.setPortraitLabel(list);
                System.out.println("list: " + list);
                List<String> nameList = userPortraitMapper.selectResultVo(list);
                basicUsersManVo.setPortraitName(nameList);
            }
            basicUsersManVos.add(basicUsersManVo);
        }

        PageInfo<BasicUsersManVo> pageInfo = new PageInfo<>(basicUsersManVos);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return CommonResult.success(resultMap);
    }

    @Override
    public CommonResult gteUsers(String id) {
        BasicUsersMan usersMan = usersManMapper.selectByPrimaryKey(id);
        return CommonResult.success(usersMan);
    }

    @Override
    public CommonResult edit(UsersManParam param) {
        if (param.getId() == null || param.getId().equals("") || param.getId().length() <= 0) {
            System.out.println("新增");
        }
        BasicUsersMan usersMan = new BasicUsersMan();
        BeanUtils.copyProperties(param, usersMan);
        if (param.getPortraitLabel() != null && param.getPortraitLabel().length > 0) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < param.getPortraitLabel().length; i++) {
                String s = param.getPortraitLabel()[i];
                if (param.getPortraitLabel().length - 1 != i) {
                    sb.append(s).append(",");
                } else {
                    sb.append(s);
                }
            }
            usersMan.setPortraitLabel(new String(sb));
        }
        int row = usersManMapper.updateByPrimaryKeySelective(usersMan);
        return result(row, "修改失败");
    }

    @Override
    public CommonResult delete(String id) {
        int row = usersManMapper.deleteByPrimaryKey(id);
        return result(row, "删除失败");
    }


    /****
     * 获取用户评论
     * @param param
     * @return
     */
    @Override
    public CommonResult getDiscuss(DiscussContentParam param) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<DiscussContentVo> dataDiscussList = dataDiscussMapper.selectByPlatformUserId(param.getPlatformUserId());
        PageInfo<DiscussContentVo> pageInfo = new PageInfo<>(dataDiscussList);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return CommonResult.success(resultMap);
    }



    /***
     * 获取用户评论
     * @param param
     * @return
     */
    @Override
    public CommonResult getContent(DiscussContentParam param) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SpsDataContentVo> contentList = dataContentMapper.selectByPlatformUserId(param.getPlatformUserId());
        PageInfo<SpsDataContentVo> pageInfo = new PageInfo<>(contentList);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return CommonResult.success(resultMap);
    }


    /***
     * 判断修改数据库返回值 返回
     * @param row
     * @param msg
     * @return
     */
    private CommonResult result(int row, String msg) {
        if (row > 0) {
            return CommonResult.success(null);
        }
        return CommonResult.failed(msg);
    }
}
